<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
        body {
            margin: 0;
        }
    </style>

</head>
<body>
<div id="app">

    <!--搜索表单-->
    <el-form :inline="true" :model="brand" class="demo-form-inline">

        <el-form-item label="当前状态">
            <el-select v-model="brand.status" placeholder="当前状态">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="企业名称">
            <el-input v-model="brand.companyName" placeholder="企业名称"></el-input>
        </el-form-item>

        <el-form-item label="品牌名称">
            <el-input v-model="brand.brandName" placeholder="品牌名称"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>

    <!--按钮-->
    <el-row>
        <el-button type="danger" plain @click="deleteByIds">批量删除</el-button>
        <el-button type="primary" plain @click="handleAdd">新增</el-button>
    </el-row>
    <!--添加数据对话框表单-->
    <el-dialog
            title="编辑品牌"
            :visible.sync="addDialogVisible"
            width="30%"
            >

        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>

            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>

            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>

            <el-form-item label="备注">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>

            <el-form-item label="状态">
                <el-switch v-model="brand.status"
                           active-value="1"
                           inactive-value="0"
                ></el-switch>
            </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="addBrand">提交</el-button>
                <el-button @click="addDialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>

    <!-- 修改数据对话框表单-->
    <el-dialog
            title="修改品牌"
            :visible.sync="updateDialogVisible"
            width="30%"
    >

        <el-form ref="form" :model="editBrand" label-width="80px">
            <el-form-item label="ID" v-if="false">
                <el-input v-model="editBrand.id" ></el-input>
            </el-form-item>
            <el-form-item label="品牌名称">
                <el-input v-model="editBrand.brandName" v-value="editBrand.brandName"></el-input>
            </el-form-item>

            <el-form-item label="企业名称">
                <el-input v-model="editBrand.companyName" v-value="editBrand.companyName"></el-input>
            </el-form-item>

            <el-form-item label="排序">
                <el-input v-model="editBrand.ordered" v-value="editBrand.ordered"></el-input>
            </el-form-item>

            <el-form-item label="描述">
                <el-input type="textarea" v-model="editBrand.description" v-value="editBrand.description"></el-input>
            </el-form-item>

            <el-form-item label="状态">
                <el-switch v-model="editBrand.status"
                           active-value="1"
                           inactive-value="0"
                           v-value="editBrand.status"
                ></el-switch>
            </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="updateInfo">提交</el-button>
                <el-button @click="unUpdateInfo">取消</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>

    <!--表格-->
    <template>
        <el-table
                :data="tableData"
                v-loading="loading"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange"
        >
            <!-- 选择框-->
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <!-- 数字序号-->
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>

            <el-table-column
                    prop="id"
                    label="ID"
                    align="center"
                    v-if="false"
            >
            </el-table-column>
            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    align="center"
                    label="排序">
            </el-table-column>
            <el-table-column
                    prop="statusStr"
                    align="center"
                    label="当前状态">
            </el-table-column>
            <el-table-column
                    prop="description"
                    align="center"
                    label="描述">
            </el-table-column>

            <el-table-column
                    align="center"
                    label="操作">

                <template slot-scope="scope">
                    <el-button type="primary" @click="BeforeUpdate(scope.row)">修改</el-button>
                    <el-button type="danger" @click="Delete(scope.row)" >删除</el-button>
                </template>
            </el-table-column>

        </el-table>
    </template>

    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount">
    </el-pagination>

</div>


<script src="js/vue.js"></script>
<script src="js/axios-0.18.0.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">

<script>
    new Vue({
        el: "#app",
        //  1.页面加载完成后执行方法
        mounted(){
            //当页面加载完成后，发送异步请求，获取数据
            this.selectAll();
            this.loading = false;
        },
        methods: {
            //  批量删除
            deleteByIds(){
                //  避免误操作，弹出确认消息弹框
                this.$confirm('此操作将删除被选中的数据，是否继续？','提示',{
                    confirmButtonText:'继续',
                    cancelButtonText:'取消',
                    type:'warning'
                }).then(() =>{
                    //  获取id数组
                    for (let i = 0; i <this.multipleSelection.length ; i++) {
                        let selection = this.multipleSelection[i];
                        this.selectedIds[i] = selection.id;
                    }
                    var _this = this;
                    //  发送ajax请求
                    axios({
                        method:"post",
                        url:"http://localhost:8080/brand-case/brandServlet/deleteByIds",
                        data:_this.selectedIds
                    }).then(resp =>{
                        if (resp.data === "success"){
                            _this.selectAll();
                            _this.$message({
                                message:'恭喜你，删除成功',
                                type:'success'
                            });
                        }
                    })
                }).catch(()=>{
                    this.$message({
                        type:'info',
                        message:'已取消批量删除'
                    })
                })
            },

            //  删除单个数据
            Delete(row){
                this.brand.id = row.id;
                var _this = this;
                this.$confirm('此操作将删除该数据，是否继续？','提示',{
                    confirmButtonText:'继续',
                    cancelButtonText:'取消',
                    type:'warning'
                }).then(() =>{
                    axios({
                        method:"get",
                        url:"http://localhost:8080/brand-case/brandServlet/deleteById?id=" + _this.brand.id
                    }).then(resp =>{
                        if (resp.data === "success"){
                            _this.selectAll();
                            _this.$message({
                                message:'恭喜你，删除成功',
                                type:'success'
                            });
                        }
                    })
                }).catch(()=>{
                    _this.$message({
                        type:'info',
                        message:'删除取消'
                    })
                })
            },

            //  修改表单数据回显
            BeforeUpdate(row) {
                this.updateDialogVisible = true;
                var _this = this;
                _this.editBrand.id = row.id;
                //2.发送ajax请求
                axios({
                    method:"get",
                    url:"http://localhost:8080/brand-case/brandServlet/selectById?id=" + _this.editBrand.id,
                }).then(function (resp) {
                    _this.editBrand = resp.data;
                })
            },

            // 修改信息
            updateInfo(){
                var _this = this;
                //2.发送ajax请求
                axios({
                    method:"post",
                    url:"http://localhost:8080/brand-case/brandServlet/update",
                    data:_this.editBrand
                }).then(function (resp) {
                    if (resp.data == "success") {
                        _this.updateDialogVisible = false;
                        // 重新查询数据
                        _this.selectAll();
                        this.loading = false;
                        // 弹出消息提示
                        _this.$message({
                            message: '恭喜您，修改成功',
                            type: 'success'
                        });
                    }
                });
            },

            unUpdateInfo(){
                this.updateDialogVisible = false;
                this.$message({
                    message: '已取消修改',
                    type: 'info'
                });
            },

            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },

            // 复选框选中后执行的方法
            handleSelectionChange(val) {
                //  复选框选中后的brand对象数组
                this.multipleSelection = val;

                console.log(this.multipleSelection)
            },

            // 条件查询
            onSubmit() {
                // console.log(this.brand);
                this.selectAll();
            },

            //新增数据对话框
            handleAdd(){
                this.addDialogVisible = true;
                this.resetForm();
            },

            // 添加数据
            addBrand(){
                var _this = this;
                //2.发送ajax请求
                axios({
                    method:"post",
                    url:"http://localhost:8080/brand-case/brandServlet/add",
                    data:_this.brand
                }).then(function (resp) {
                    if (resp.data == "success"){
                        _this.addDialogVisible = false;
                        // 重新查询数据
                        _this.selectAll();
                        this.loading = false;
                        // 弹出消息提示
                        _this.$message({
                            message: '恭喜您，添加成功',
                            type: 'success'
                        });
                    }
                })
            },
            //  重置表单
            resetForm(){
                this.brand = {};
            },

            //分页
            handleSizeChange(val) {
                this.pageSize = val;
                this.selectAll();
            },

            handleCurrentChange(val) {
                this.currentPage = val;
                this.selectAll();
            },

            //  查询所有改进成分页查询
            selectAll(){
                axios({
                    method:"post",
                    url:"http://localhost:8080/brand-case/brandServlet/selectByPageAndCondition?currentPage="+this.currentPage+"&pageSize="+this.pageSize,
                    data:this.brand
                }).then(resp =>{
                    //设置表格数据
                    this.tableData = resp.data.rows; // {rows:[],totalCount:100}
                    //设置总记录数
                    this.totalCount = resp.data.totalCount;
                })
            },

        },
        data() {
            return {
                //  复选框被选中的id数组
                selectedIds:[],

                pageSize:5,

                currentPage:1,

                //  展示总记录
                totalCount:100,

                //  修改数据对话框是否展示的标记
                updateDialogVisible: false,

                //  添加对话框标记
                addDialogVisible: false,

                // 品牌模型数据
                brand: {
                    status: '',
                    brandName: '',
                    companyName: '',
                    id:"",
                    ordered:"",
                    description:""
                },
                editBrand: {
                    status: '',
                    brandName: '',
                    companyName: '',
                    id:"",
                    ordered:"",
                    description:""
                },
                // 复选框选中数据集合
                multipleSelection: [],
                // 表格数据
                tableData: [{
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1",
                    id:"",
                    description:"好"
                }, {
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1",
                    id:"",
                    description:"好"
                }, {
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "0",
                    id:"",
                    description:"好"
                }, {
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1",
                    id:"",
                    description:"好"
                }],
                // 加载模型标识
                loading: true
            }
        }
    })

</script>

</body>
</html>